package au.org.arcs.dss.provider.datasource.provider.irods;

import java.util.*;

import edu.sdsc.grid.io.irods.IRODSFile;
import edu.sdsc.grid.io.GeneralFile;

import au.org.arcs.dss.provider.dataset.DataSet;
import au.org.arcs.dss.provider.datasource.provider.DataSourceProvider;
import au.org.arcs.dss.provider.datasource.provider.ProviderAccount;

/**
 *
 * @author ming
 */
public class DataSourceProviderIRODSImpl implements DataSourceProvider {
  private ProviderAccount providerAccount;

  public DataSet getRootDataSet(String path, boolean withData) {
    IRODSService irodsService = new IRODSService(providerAccount);

    IRODSFile irodsFile = irodsService.getIRODSFile(path, false);
    DataSet dataSet = IRODSDataSetAdapter.getDataSet(irodsFile, false, true, false);

    irodsService.close();

    return dataSet;
  }

  public DataSet getDataSetByPathAndName(String path, String name, boolean withData, boolean withDecendents, boolean withParent) {
    IRODSService irodsService = new IRODSService(providerAccount);
    IRODSFile irodsFile = null;

    irodsFile = irodsService.getIRODSFile(IRODSService.getIrodsPath(path, name), withDecendents);

    DataSet dataSet =  IRODSDataSetAdapter.getDataSet(irodsFile, withParent, withDecendents, withData);
    irodsService.close();

    return dataSet;
  }

  public void setProviderAccount(ProviderAccount providerAccount) {
    this.providerAccount = providerAccount;
  }

  public List<DataSet> getDataSetsByPath(String path, boolean withData) {
    List<DataSet> dataSets = new ArrayList<DataSet>();
    IRODSService irodsService = new IRODSService(providerAccount);
    IRODSFile irodsFile = irodsService.getIRODSFile(path, false);

    //List<DataSet> dataSets = IRODSDataSetAdapter.getDataSets(Arrays.asList(irodsFile.listFiles()), withData);
    
    dataSets.addAll(IRODSDataSetAdapter.getDataSets(irodsService.listSubFiles(irodsFile), false));
    dataSets.addAll(IRODSDataSetAdapter.getDataSets(irodsService.listSubDirectories(irodsFile), true));

    irodsService.close();

    return dataSets;
  }
}